from PIL import Image
import os,glob,json,cv2,numpy as np
import matplotlib.pyplot as plt
from  tqdm import tqdm

#-----------------------------------------------------------------------
#   Visualize the marked data to facilitate viewing of abnormal data.
#             please check save_path  and json_file
#-----------------------------------------------------------------------

def data_visualization():
    save_path="/data2/enducation/datas/answer_card/unet_data/data_clean"
    json_file="/data2/enducation/datas/answer_card/unet_data/biaozhu_unet"
    pbar=tqdm(glob.glob(os.path.join(json_file, "*.json")))
    for json_path in pbar:
        pbar.set_description("Processing%s"%" Datacleaning")
        _,img_name=os.path.split(json_path)
        img_name=img_name[:-5]
        image_path=json_path.replace("json","jpg" or "png")
        if os.path.isfile(image_path):
            img=cv2.imread(image_path,0)
        if json_path.endswith(".json") :
            if os.path.isfile(json_path):
                data = json.load(open(json_path))
                for cls in data["shapes"]:
                    if cls["label"]=="id":
                        points=np.array(cls["points"],dtype=np.int32)
                        cv2.polylines(img,[points],True,(0,255,255),5)
                    elif cls["label"]=="answer":
                        points=np.array(cls["points"],dtype=np.int32)
                        cv2.polylines(img,[points],True,(255,0,255),5)
                    else:
                        points = np.array(cls["points"], dtype=np.int32)
                        cv2.polylines(img, [points], True, (0, 255, 255), 5)
        # cv2.imshow("sad",img)
        # cv2.waitKey(0)
        # cv2.destroyAllWindows()
        cv2.imwrite(os.path.join(save_path,f"{img_name}.jpg"),img)
    print("Data cleaning completed! Please view in file<%s>"%save_path)



if __name__ == '__main__':

    data_visualization()


